
//
//  ZRNavigationController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/11/30.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "ZRNavigationController.h"

@interface ZRNavigationController () <UINavigationControllerDelegate>

@end

@implementation ZRNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.PopDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;

//    [self setNavigationBarBackgroundClear];
    
    
    [self setTitleTextColor:[UIColor whiteColor]];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/** 设置title文字颜色 */
- (void)setTitleTextColor:(UIColor *)color{
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    
    self.navigationBar.titleTextAttributes= dict;
}

/** 设置导航条透明 */
- (void)setNavigationBarBackgroundClear{
    
    self.navigationBar.translucent = YES;
    UIColor *color = COLOR_CLEAR;
    CGRect rect = CGRectMake(0.0f, 0.0f, SCREEN_W, 64);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.clipsToBounds = YES;
    
}
/** 解决返回手势失效问题 */
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (viewController == self.viewControllers[0]) {
        self.interactivePopGestureRecognizer.delegate = self.PopDelegate;
    }else{
        self.interactivePopGestureRecognizer.delegate = nil;
    }
}

/** push时隐藏tabBar */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    [super pushViewController:viewController animated:animated];
    
    if (self.viewControllers.count > 0) {
        [UIView animateWithDuration:0.1 animations:^{
            [AppDelegate shareDelegate].tabBar.customBar.center = CGPointMake(SCREEN_W / 2, SCREEN_H + [AppDelegate shareDelegate].tabBar.customBar.frame.size.height / 2);
        }];
    }
    
    //设置返回按钮
    if (viewController.navigationItem.leftBarButtonItem == nil && self.viewControllers.count >1) {
        viewController.navigationItem.leftBarButtonItem = [self creatBackButton];
    }
}

/** 设置返回按钮 */
- (UIBarButtonItem *)creatBackButton{
    
    UIImage *image = ImageNamed(@"back");
    //防止image颜色被改变
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemAction)];

}

- (void)backBarButtonItemAction
{
    [self popViewControllerAnimated:YES];
}

/** 设置状态栏样式 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}




@end
